﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Register.Model;
using Register.DBUtility;
using Register.IDAL;
using Register.Command;
using System.Data;
using System.Data.SqlClient;
namespace Register.DAL
{
    /// <summary> 
    /// FileName: OutpatientDAL.cs    
    /// CLRVersion: 4.0.30319.269    
    /// Author: DoubleZ    
    /// Corporation:     
    /// Description:     
    /// DateTime: 2012/7/10 10:46:03    
    /// </summary>
  public  class OutpatientDAL:IOutpatientDAL
  {
      private string tableName = "System_register";
      #region 获取患者就诊信息
      public List<Model.RegisterInfo> GetPagedOutpatientInfo(int size, int pageIndex, string where) {
          return DataSetToList(DbHelperSQL.getPagedData(" * ", "Register_AddTime", where, tableName, OrderBy.DESC, pageIndex, size));
      }

      public int GetOutpatientInfoCount(string where) {
          int count = 0;
          StringBuilder str = new StringBuilder();
          str.Append(" select count(*) from ");
          str.Append(tableName);
          if (where != null)
          {
              str.Append(" where ");
              str.Append(where);
              count = (int)DbHelperSQL.GetSingle(str.ToString());
          }
          else
          {
              count = (int)DbHelperSQL.GetSingle(str.ToString());
          }
          return count;
      
      }
      private List<Model.RegisterInfo> DataSetToList(DataSet ds)
      {
          List<Model.RegisterInfo> list = new List<Model.RegisterInfo>();
          foreach (DataRow dr in ds.Tables[0].Rows)
          {
              RegisterInfo register = new RegisterInfo();
              register.Register_ID = (string)dr["Register_ID"];
              register.Register_Number =(int) dr["Register_Number"];
              register.Register_IDCode = (string)dr["Register_IDCode"];
              register.Register_PatientName = (string)dr["Register_PatientName"];
              register.Register_PaintSex = (string)dr["Register_PaintSex"];
              register.Register_BirthDay = (DateTime)dr["Register_BirthDay"];
              register.Register_IDCards = (string)dr["Register_IDCards"];
              register.Register_IsClinic = (string)dr["Register_IsClinic"];
              list.Add(register);
          }
          return list;
      }
      #endregion
      #region 添加患者就诊信息
     public int AddOutpatientInfo(Model.Outpatient patient) {
         StringBuilder strSql = new StringBuilder();
         strSql.Append("insert into System_outpatient(");
         strSql.Append("Outpatient_ID,Register_ID,Outpatient_PaitentName,Outpatient_PaitentSex,Outpatient_PaitentBirthDay,Outpatient_PaitentIDCards,Outpatient_OutpaitentType,Outpatient_AddTime,Outpatient_Recoder,Outpatient_MainIntroduce,Outpatient_ShowHistory,Outpatient_PastHistory,Outpatient_IsClinic)");
         strSql.Append(" values (");
         strSql.Append("@Outpatient_ID,@Register_ID,@Outpatient_PaitentName,@Outpatient_PaitentSex,@Outpatient_PaitentBirthDay,@Outpatient_PaitentIDCards,@Outpatient_OutpaitentType,@Outpatient_AddTime,@Outpatient_Recoder,@Outpatient_MainIntroduce,@Outpatient_ShowHistory,@Outpatient_PastHistory,@Outpatient_IsClinic)");
         SqlParameter[] parameters = {
					new SqlParameter("@Outpatient_ID", SqlDbType.Char,14),
					new SqlParameter("@Register_ID", SqlDbType.Char,14),
					new SqlParameter("@Outpatient_PaitentName", SqlDbType.VarChar,20),
					new SqlParameter("@Outpatient_PaitentSex", SqlDbType.Char,2),
					new SqlParameter("@Outpatient_PaitentBirthDay", SqlDbType.DateTime),
					new SqlParameter("@Outpatient_PaitentIDCards", SqlDbType.Char,18),
					new SqlParameter("@Outpatient_OutpaitentType", SqlDbType.Char,4),
                    new SqlParameter("@Outpatient_AddTime", SqlDbType.DateTime),
                    new SqlParameter("@Outpatient_Recoder", SqlDbType.VarChar,20),
                    new SqlParameter("@Outpatient_MainIntroduce", SqlDbType.VarChar,100),
                    new SqlParameter("@Outpatient_ShowHistory", SqlDbType.VarChar,200),
                    new SqlParameter("@Outpatient_PastHistory", SqlDbType.VarChar,200),
                     new SqlParameter("@Outpatient_IsClinic", SqlDbType.Char,1)
                                      };
         parameters[0].Value = patient.Outpatient_ID;
         parameters[1].Value = patient.Register_ID;
         parameters[2].Value = patient.Outpatient_PaitentName;
         parameters[3].Value = patient.Outpatient_PaitentSex;
         parameters[4].Value = patient.Outpatient_PaitentBirthDay;
         parameters[5].Value = patient.Outpatient_PaitentIDCards;
         parameters[6].Value = patient.Outpatient_OutpaitentType;
         parameters[7].Value = patient.Outpatient_AddTime;
         parameters[8].Value = patient.Outpatient_Recoder;
         parameters[9].Value = patient.Outpatient_MainIntroduce;
         parameters[10].Value = patient.Outpatient_ShowHistory;
         parameters[11].Value = patient.Outpatient_PastHistory;
         parameters[12].Value = patient.Outpatient_IsClinic;
         

         int flag = (int)DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
         //如果添加成功，更新挂号表中的门诊ID
         if (flag > 0)
         {

             string strSql1 = "update System_register set Outpatient_ID='" + patient.Outpatient_ID + "',Register_IsClinic=1 where Register_ID='" +patient.Register_ID + "'";
             return DbHelperSQL.ExecuteSql(strSql1);
         }
         else return -1;
          
      
      }
      #endregion
      #region 检查患者是否已就诊
    public bool Exists(string register_ID) {

        string strSql = "select  Register_IsClinic from System_register where Register_ID='" +register_ID + "'";
        DataSet ds = DbHelperSQL.Query(strSql);

        if (ds != null)
        {    //已就诊
            if (ds.Tables[0].Rows[0]["Register_IsClinic"].ToString().Equals("1"))
            {
                return true;
            }

            else { return false; }

        }
        else
        { return false; }
    }
      #endregion
      #region 根据医生编号获取医生对应的门诊类型
    public string GetOutpatientType(string doctorID) {
        string strSql = "select SchedulingAdjustment_OutpaientType  from System_schedulingAdjustment  where  SchedulingInfo_ID='" + doctorID + "'";
            DataSet ds = DbHelperSQL.Query(strSql);
            string SchedulingAdjustment_OutpaientType = "";
            if (ds != null)
            {
                SchedulingAdjustment_OutpaientType = ds.Tables[0].Rows[0]["SchedulingAdjustment_OutpaientType"].ToString();

            }
            return SchedulingAdjustment_OutpaientType;
        }

    
      #endregion
      #region 查看就诊信息
   public List<Model.Outpatient> GetOutpatientInfo(string outpatientID) {
       string strSql = "select * from System_outpatient where Register_ID='" + outpatientID + "'";
       return DataSetToList1(DbHelperSQL.Query(strSql));

    }


   private List<Model.Outpatient> DataSetToList1(DataSet ds)
   {
       List<Model.Outpatient> list = new List<Model.Outpatient>();
       foreach (DataRow dr in ds.Tables[0].Rows)
       {
           Outpatient patient = new Outpatient();
           patient.Outpatient_AddTime = (DateTime)dr["Outpatient_AddTime"];
           patient.Outpatient_MainIntroduce = (string)dr["Outpatient_MainIntroduce"];
           patient.Outpatient_OutpaitentType = (string)dr["Outpatient_OutpaitentType"];
           patient.Outpatient_PaitentBirthDay = (DateTime)dr["Outpatient_PaitentBirthDay"];
           patient.Outpatient_PaitentIDCards = (string)dr["Outpatient_PaitentIDCards"];
           patient.Outpatient_PaitentName = (string)dr["Outpatient_PaitentName"];
           patient.Outpatient_PaitentSex = (string)dr["Outpatient_PaitentSex"];
           patient.Outpatient_PastHistory = (string)dr["Outpatient_PastHistory"];
           patient.Outpatient_Recoder = (string)dr["Outpatient_Recoder"];
           patient.Outpatient_ShowHistory = (string)dr["Outpatient_ShowHistory"];
           patient.Outpatient_IsClinic = (string)dr["Outpatient_IsClinic"];
           patient.Outpatient_ID = (string)dr["Outpatient_ID"];
           patient.Register_ID = (string)dr["Register_ID"];
           list.Add(patient);
       }
       return list;
   }
      #endregion
      #region 多条件查询
  public List<Model.Outpatient> GetPagedSearchInfo(int size, int pageIndex, string where) {
      return DataSetToList1(DbHelperSQL.getPagedData(" * ", "Outpatient_AddTime", where, "System_outpatient", OrderBy.DESC, pageIndex, size));
   }

  public int GetSearchCount(string where) {
      int count = 0;
      StringBuilder str = new StringBuilder();
      str.Append(" select count(*) from System_outpatient");
     
      if (where != null)
      {
          str.Append(" where ");
          str.Append(where);
          count = (int)DbHelperSQL.GetSingle(str.ToString());
      }
      else
      {
          count = (int)DbHelperSQL.GetSingle(str.ToString());
      }
      return count;
   
  }

      #endregion
  }
}
